package cn.stylefeng.guns.modular.standard;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.stylefeng.guns.comm.util.Base64Util;
import cn.stylefeng.guns.comm.util.FileUtil;
import cn.stylefeng.guns.comm.util.GsonUtils;
import cn.stylefeng.guns.comm.util.HttpUtil;
import com.wxapi.WxApiCall.WxApiCall;
import com.wxapi.model.RequestModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CloudApi {

    public static String imgCombination() {
        // 请求url
        String url = "https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("imgUrl", "https://6372-cr-b1d4d0-5c2616-1258957886.tcb.qcloud.la/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20210302212322.jpg");
            List<Object> scenes = new ArrayList<>();
            scenes.add("advanced_general");
            map.put("scenes", scenes);

            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.1f1c0b5fec24d5ea6f6d15a0b2992215.2592000.1617282972.282335-23732330";

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String advancedGeneral() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
        try {
            // 本地文件路径
            String filePath = "C:\\Users\\17300\\Pictures\\3062c0fd1eb2196ee34b3d35c97de758.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.1f1c0b5fec24d5ea6f6d15a0b2992215.2592000.1617282972.282335-23732330";

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String garbageImageSearch() throws Exception {

        String filePath = "C:\\Users\\17300\\Pictures\\3062c0fd1eb2196ee34b3d35c97de758.jpg";
        byte[] imgData = FileUtil.readFileByBytes(filePath);
        String imgStr = Base64Util.encode(imgData);

        RequestModel model = new RequestModel();
        model.setGwUrl("https://way.jd.com/JDAI/garbageImageSearch");
        model.setAppkey("1ef6256d2d6f092b83b4572bdb2ed9e4");
        model.setBodyStr("{\"cityId\":\"310000\",\"imgBase64\":\"" + imgStr + "\"}");    //body参数
        Map queryMap = new HashMap();
        model.setQueryParams(queryMap);
        WxApiCall call = new WxApiCall();
        call.setModel(model);
        String result = call.request();

//        String url = "https://way.jd.com/JDAI/garbageImageSearch?appkey=1ef6256d2d6f092b83b4572bdb2ed9e4";
//        Map<String, Object> map1 = new HashMap<>();
//        Map<String, Object> map = new HashMap<>();
//        map1.put("body", map);
//        map.put("cityId", "310000");
//        String filePath = "C:\\Users\\17300\\Pictures\\3062c0fd1eb2196ee34b3d35c97de758.jpg";
//        byte[] imgData = FileUtil.readFileByBytes(filePath);
//        String imgStr = Base64Util.encode(imgData);
//        String imgParam = URLEncoder.encode(imgStr, "UTF-8");
//        map.put("imgBase64", imgParam);
//        String param = GsonUtils.toJson(map1);
//        System.out.println(imgParam);
//        String result = HttpUtil.postGeneralUrl(url, "application/json", param, "UTF-8");
        System.out.println(result);
        return result;
    }

    public static void main(String args[]) throws Exception {
        garbageImageSearch();
        //advancedGeneral();
        //imgCombination();


//        final WxMaService wxService = getWxMaService();
//        UploadFileParam uploadFileParam = new UploadFileParam();
//        uploadFileParam.setEnv("fu-gong-yi-kong-sib7c");
//        uploadFileParam.setPath("standard/gb/");
//        String responseContent = wxService.post("https://api.weixin.qq.com/tcb/uploadfile", uploadFileParam.toJson());
//        UploadFileResult uploadFileResult = UploadFileResult.fromJson(responseContent);
//        System.out.println(responseContent);
//        // 第二步
//        Map<String, Object> keyValues = new HashMap<>();
//        keyValues.put("key", uploadFileParam.getPath());
//        keyValues.put("Signature", uploadFileResult.getAuthorization());
//        keyValues.put("x-cos-security-token", uploadFileResult.getToken());
//        keyValues.put("x-cos-meta-fileid", uploadFileResult.getCosFileId());
//        Map<String, String> filePathMap = new HashMap<>();
//        String paramName = "file";
//        String filePath = "D:\\standard\\20200221122906.rar";
//        filePathMap.put(paramName, filePath);
//        HttpResponse response = HttpUtils.postFormData(uploadFileResult.getUrl(), filePathMap, keyValues, null);
//        System.out.println(response);

    }

    public static byte[] fileToByte(String filePath) throws IOException {
        byte[] bytes = null;
        FileInputStream fis = null;
        try {
            File file = new File(filePath);
            fis = new FileInputStream(file);
            bytes = new byte[(int) file.length()];
            fis.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            fis.close();
        }
        return bytes;
    }

    private static WxMaService getWxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        // 复工疫控宝
        config.setAppid("wx7f1bdf77e82bbecb");
        config.setSecret("f4d1505f9548039347ee6d6bcde7fe1f");
        config.setMsgDataFormat("JSON");
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }
}
